<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>部门管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
    <div th:replace="common/fragment :: link"></div>
    <!-- bootstrap-table 表格插件 -->
    <link href="/static/js/plugins/bootstrap-table/bootstrap-table.min.css?
                v=20210202" rel="stylesheet"/>
    <script src="/static/js/plugins/bootstrap-table/bootstrap-table.min.js?
                v=20210202"></script>
    <script src="/static/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js?v=20210202"></script>
    <script src="/static/js/plugins/bootstrap-table/extensions/mobile/bootstrap-table-mobile.min.js?v=20210202"></script>

    <div class="wrapper">
        <div th:replace="common/fragment :: navbar"></div>
        <div th:replace="common/fragment :: menu"></div>
        <div class="content-wrapper">
            <section class="content-header">
                <h1>部门管理</h1>
            </section>
            <section class="content">
                <div class="box">
                    <!--高级查询--->
                    <form class="form-inline" id="searchForm" action="/department/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <input class="form-control" type="text" name="keyword" placeholder="关键字">
                        <a href="#" class="btn btn-success" onclick="searchOp()">
                            <span class="glyphicon glyphicon-search"></span> 查询
                        </a>
                        <a href="#" class="btn btn-success btn-input" style="margin: 10px" onclick="addOp()" shiro:hasPermission="department:saveOrUpdate">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="/department/export" class="btn btn-warning" style="margin: 10px">
                            <span class="glyphicon glyphicon glyphicon glyphicon-download"></span> 导出
                        </a>
                        <a href="#" class="btn btn-warning" onclick="importOp()" style="margin: 10px">
                            <span class="glyphicon glyphicon glyphicon glyphicon-upload"></span> 导出
                        </a>
                    </form>
                    <!--编写内容-->
                    <div class="box-body table-responsive ">
                        <table id="table"></table>
                        <script>
                            $('#table').bootstrapTable({
                                url: '/department/listData',
                                method: 'GET', //数据请求方式
                                sidePagination:'server',//服务端分页
                                pagination:true,//开启分页
                                pageNumber:1, //当前地基页
                                pageSize:5, //每页显示数据条数
                                uniqueId:"id",
                                columns: [{
                                    field: 'id',
                                    title: '编号'
                                }, {
                                    field: 'name',
                                    title: '名称'
                                }, {
                                    field: 'sn',
                                    title: '缩写'
                                }, {
                                    title: '操作',
                                    align: 'center',
                                    queryParams:function(params) {
                                        params.keyword=$("[name=keyword]").val();
                                        return params;
                                    },
                                    formatter: function(value, row, index) {
                                        var actions = [];
                                        actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="editOp('+row.id+')"><i class="fa fa-edit"></i>编辑</a> ');
                                        actions.push('<a class="btn btn-danger btn-xs btn-delete" href="javascript:void(0)" onclick="deleteOp('+row.id+')"><i class="fa fa-remove"></i> 删除</a> ');
                                        return actions.join('');
                                    }
                                }]
                            });
                            function searchOp(){
                                $('#table').bootstrapTable('refresh');
                            }
                            function addOp() {
                                $('#myModal input[name]').val('');
                                $("#myModalLabel").html("部门新增");
                                $('#myModal').modal('show');
                            }
                            function editOp(id){
                                var row = $('#table').bootstrapTable('getRowByUniqueId', id);
                                $('#myModal input[name]').val('');
                                $('input[name=id]').val(row.id);
                                $('input[name=name]').val(row.name);
                                $('input[name=sn]').val(row.sn);
                                $("#myModalLabel").html("部门修改");
                                $('#myModal').modal('show');
                            }
                            function saveOp(){
                                $.ajax({
                                    url: "/department/saveOrUpdate",
                                    type: "post",
                                    dataType: "json",
                                    data: $('#selfForm').serialize(),
                                    success: function(data) {
                                        if(data.success){
                                            $('#table').bootstrapTable('refresh');
                                            $('#myModal').modal('hide');
                                        }else{
                                            Swal.fire({
                                                text: data.data,
                                                icon: 'warning'
                                            })
                                        }
                                    }
                                });
                            }
                            function deleteOp(id){
                                Swal.fire({
                                    title: '您确定要删除吗？',
                                    text: "此操作不可撤销!",
                                    icon: 'warning',
                                    showCancelButton: true,
                                    confirmButtonColor: '#3085d6',
                                    cancelButtonColor: '#d33',
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消'
                                }).then((result) => {
                                    if(result.value) {
                                        $.ajax({
                                            url: "/department/delete?id="+id,
                                            type: "get",
                                            dataType: "json",
                                            success: function(data) {
                                                if(data.success){
                                                    $('#table').bootstrapTable('refresh');
                                                }else{
                                                    Swal.fire({
                                                        text: data.msg,
                                                        icon: 'warning'
                                                    })
                                                }
                                            }
                                        })
                                    }
                                });
                            }
                        </script>
                    </div>
                </div>
            </section>
        </div>
        <div th:replace="common/fragment :: footer"></div>
    </div>
    <div class="modal fade" tabindex="-1" role="dialog" id="myModal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">部门新增/修改</h4>
                </div>
                <form action="#" id="selfForm" method="post">
                    <input type="hidden" name="id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="name">名称</label>
                            <input type="text" class="form-control" name="name" id="name" placeholder="名称">
                        </div>
                        <div class="form-group">
                            <label for="sn">缩写</label>
                            <input type="text" class="form-control" name="sn" id="sn" placeholder="缩写">
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary" onclick="saveOp()">保存</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">导入</h4>
                </div>
                <form class="form-horizontal" enctype="multipart/form-data"
                      method="post" id="importForm">
                    <div class="modal-body">
                        <div class="form-group" style="margin-top: 10px;">
                            <label for="uploadFile" class="col-sm-3 control-label">
                            </label>
                            <div class="col-sm-6">
                                <!-- 文件上传框 -->
                                <input id="uploadFile" type="file" name="file" multiple/>
                            </div>
                        </div>
                        <div class="form-group" style="margin-top: 10px;">
                            <div class="col-sm-3"></div>
                            <div class="col-sm-6">
                                <a href="/department/downloadTemplate" class="btn btn-success" >
                                    <span class="glyphicon glyphicon-download"></span> 下载模板
                                </a>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button type="button" onclick="importSave()" class="btn btn-primary btn-submit">保存</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script>
        function importOp(){
            $("#importModal").modal("show");
        }
        function importSave(){
            var $file1 = $("#uploadFile").val();
            //用户文件内容(文件)
            // 判断文件是否为空
            if ($file1 == "") {
                Swal.fire({
                    text: "请选择上传的目标文件! ",
                    icon: 'warning',
                });
                return false;
            }
            var formData = new FormData();
            //这里需要实例化一个FormData来进行文件上传
            formData.append("file",$("#uploadFile")[0].files[0]);
            $.ajax({
                type : "post",
                url : "/department/importExcel",
                data : formData,
                processData : false,
                contentType : false,
                success : function(data){
                    if (data.success) {
                        Swal.fire({
                            text: data.msg,
                            icon: 'success',
                        });
                        $("#importModal").modal("hide");
                        $('#table').bootstrapTable('refresh');
                    }else{
                        Swal.fire({
                            text: "请选择上传的目标文件! ",
                            icon: 'warning',
                        })
                    }}
            });
        }

    </script>
</body>
</html>